﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Foundation;
using Foundation.Sugar;
using Vent.ViewModels;

namespace Vent
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Rainbow_OnMouseMove(object sender, MouseEventArgs e)
        {
            var palette = (Panel) sender;
            var position = e.GetPosition(palette);
            SetBrush(palette, position);
        }

        private void Rainbow_OnTap(object sender, GestureEventArgs e)
        {
            var palette = (Panel) sender;
            var position = e.GetPosition(palette);
            SetBrush(palette, position);
        }

        private static void SetBrush(FrameworkElement palette, Point position)
        {
            var tools = (Panel) VisualTreeHelper.GetParent(palette);

            if (palette.Name == "RainbowPalette")
            {
                var baseBrush = tools.GetVisualDescendants<Canvas>().First(c => c.Name == "BaseBrush");
                baseBrush.Background = GetBrush(palette, position);
                var strokeBrush = tools.GetVisualDescendants<Canvas>().First(c => c.Name == "StrokeBrush");
                strokeBrush.Background = baseBrush.Background;
                var gradientBrush = tools.GetVisualDescendants<Canvas>().First(c => c.Name == "GradientPalette");
                gradientBrush.Background =
                    new LinearGradientBrush(new GradientStopCollection
                    {
                        new GradientStop {Offset = 0.0, Color = Colors.White},
                        new GradientStop {Offset = 0.5, Color = ((SolidColorBrush) baseBrush.Background).Color},
                        new GradientStop {Offset = 1.0, Color = Colors.Black},
                    }, 0) {EndPoint = new Point(0, 1)};
            }
            else
            {
                var strokeBrush = tools.GetVisualDescendants<Canvas>().First(c => c.Name == "StrokeBrush");
                strokeBrush.Background = GetBrush(palette, position);
            }
        }

        private static Brush GetBrush(UIElement element, Point position)
        {
            var bitmap = new WriteableBitmap(element, null);
            var rawColor = GetPixel(bitmap, (int) position.X, (int) position.Y);
            var color = Color.FromArgb(
                255,
                (byte) ((((rawColor >> 16) & 0xFF))),
                (byte) ((((rawColor >> 8) & 0xFF))),
                (byte) ((((rawColor & 0xFF)))));

            return new SolidColorBrush(color);
        }

        public static int GetPixel(WriteableBitmap bitmap, int x, int y)
        {
            var index = y*bitmap.PixelWidth + x;
            if (index < 0 || index >= bitmap.Pixels.Length) return 0;
            return bitmap.Pixels[index];
        }

        private void Gradient_OnLoaded(object sender, RoutedEventArgs e)
        {
            var gradientPalette = (Canvas) sender;
            var tools = (Panel) VisualTreeHelper.GetParent(gradientPalette);
            var baseBrush = tools.GetVisualDescendants<Canvas>().First(c => c.Name == "BaseBrush");
            var background = (SolidColorBrush) baseBrush.Background ??
                             (SolidColorBrush) Store.OfType<TouchViewModel>().Tool.Stroke;
            gradientPalette.Background =
                new LinearGradientBrush(new GradientStopCollection
                {
                    new GradientStop {Offset = 0.0, Color = Colors.White},
                    new GradientStop {Offset = 0.5, Color = background.Color},
                    new GradientStop {Offset = 1.0, Color = Colors.Black},
                }, 0) {EndPoint = new Point(0, 1)};
        }
    }
}